1702C - Train and Queries - CodeForces Solution


data structures greedy *1100

Please click on ads to support us..

Python Code:

for t in range(int(input())):
    input(); m,q = map(int,input().split()); s = [*input().split()]
    k = {}; l = {}
    for i,j in enumerate(s):
        if(j not in k): k[j] = i
        l[j] = i
    for i in range(q):
        x,y = input().split()
        print("YES" if (x in k and y in l and k[x]<l[y]) else "NO")
        

C++ Code:

#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long int
#define f(i,a,b) for(long long int i=a;i<b;i++)
#define pb push_back
#define mk make_pair
#define ub upper_bound
#define lb lower_bound 
#define ss second
#define ff first
#define endl "\n"
using namespace std;
const ll mod = 1000000007;
const ll inf=-1000000001;

void solve() 
{
    
    ll n, k;
    cin>>n>>k;
    map<ll, ll> far, near;
    for (ll i=0; i<n; i++)
    {
        ll e;
        cin>>e;
        if (!near.count(e))
        {
            near[e]=i;
            far[e]=i;
        }

        else
        {
            far[e]=i;
        }
    }

    // for (auto &i:near) cout<<i.first<<" "<<i.ss<<endl;
    // cout<<endl;
    // for (auto &i:far) cout<<i.ff<<" "<<i.ss<<endl;
    // cout<<endl;

    for (ll i=0; i<k; i++)
    {
        ll f, s;
        cin>>f>>s;
        if (!near.count(f) or !near.count(s)) cout<<"NO"<<endl;
        else if (near[f]<far[s]) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;

    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin >> t;
    while (t--)
    {

        solve();
    }
}


Comments

Submit
0 Comments
More Questions

1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility
20B - Equation
600B - Queries about less or equal elements
1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares